﻿using System;
using System.Data;
using System.Configuration;
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using MySql.Data.MySqlClient;
using LdExpress;
using System.IO;
using System.Globalization;

public partial class _Default : System.Web.UI.Page
{
    public string ContainerPage = "./Prices.aspx";
    public string strCondition = "";
    public int NumOfItemsPerPage = 100;
    public string strPager = "";
    public string strIDs = "";
    public string strError = "";
    public string strMess = "";
    public int nPermissions = 0;
    public int nPermissionsGoodPrice = 0;
    public string strPortToName = "";
    public string strPortFromName = "";
    public int nPage = 1;
    public string strAlphaBet = "";
    public string strPageID = "page_number";
    public string strLetter = "letter";
    string strChar = "";
    string strExcluded = "";
    public string strPartQuery = "";
    protected bool isSearch = false;
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (Request.QueryString[strLetter] != null)
            {
                strChar = Request.QueryString[strLetter].ToString();
                strCondition = " AND CarrName like '" + strChar + "%'";
            }
        }
        catch
        {
            strChar = "";
        }
        try
        {

            if (Request.QueryString[strPageID] != null)
                nPage = int.Parse(Request.QueryString[strPageID]);
        }
        catch
        {
            nPage = 1;
        }
        if (nPage <= 0) nPage = 1;

        if (Session["logined_id"] != null)
        {
            if (Session["Prices"] != "") nPermissions = int.Parse(Session["Prices"].ToString());
            if (Session["GoodPrices"] != "") nPermissionsGoodPrice = int.Parse(Session["GoodPrices"].ToString());
            if (Session["superadmin"] != null && int.Parse(Session["superadmin"].ToString()) > 0) nPermissions = 10;
            if (nPermissions <= 0) Response.Redirect("Login.aspx");
        }
        else
            Response.Redirect("Login.aspx");
        if (!Page.IsPostBack)
        {
            if (Request.QueryString["PortToID"] != null && Request.QueryString["PortFromID"] != null) //&& Request.QueryString["pOceanID"] != null)
            {
                Ports objPol = new Ports();
                objPol.ID = long.Parse(Request.QueryString["PortToID"].ToString());
                objPol.Load(GV.cn, GV.trans);
                Session["PortToName"] = objPol.Name;
                Session["PortToID"] = objPol.ID;
                Ports objPod = new Ports();
                objPod.ID = long.Parse(Request.QueryString["PortFromID"].ToString());
                objPod.Load(GV.cn, GV.trans);
                Session["PortFromName"] = objPod.Name;
                Session["PortFromID"] = objPod.ID;
                if (Request.QueryString["action"] != null && (Request.QueryString["action"].ToString() == "allschedules_exp" || Request.QueryString["action"].ToString() == "allschedules_imp"))
                    Session["PriceTimeUpdate"] = "2000";
            }
            isSearch = LoadSearch();
            //Set Page Title
            if (Session["PortToName"].ToString().Length > 0 && Session["PortToName"].ToString().Length > 0)
            {
                if (Session["PortToName"].ToString().ToLower().IndexOf("vietnam") > 0)
                    Page.Title = Session["PortFromName"].ToString().Split(',')[0].ToString();
                else if (Session["PortFromName"].ToString().ToLower().IndexOf("vietnam") > 0)
                    Page.Title = Session["PortToName"].ToString().Split(',')[0].ToString();
                else
                    Page.Title = Session["PortToName"].ToString().Split(',')[0].ToString() + " - " + Session["PortFromName"].ToString().Split(',')[0].ToString();
            }

            CommonFunctions.GetAllQueryString(Request, "", "id", out strPartQuery);
            if (Request.QueryString["action"] == "delete" && nPermissions > 7)
            {
                Prices obj = new Prices();
                obj.ID = long.Parse(Request.QueryString["ID"]);
                obj.Delete(GV.cn, GV.trans);
                UserObjectsLog.SaveObjectActionLog(long.Parse(Session["logined_id"].ToString()), "Prices", "Delete", GV.cn, GV.trans);
                Response.Redirect("Prices.aspx" + ((Request.QueryString["page_number"] != null && Request.QueryString["page_number"].ToString() != "") ? "?page_number=" + Request.QueryString["page_number"].ToString() : ""));
            }
            if (Request.QueryString["action"] == "deleteGP" && nPermissions > 7)
            {
                GoodPrices obj = new GoodPrices();
                obj.ID = long.Parse(Request.QueryString["GID"]);
                obj.Delete(GV.cn, GV.trans);
                UserObjectsLog.SaveObjectActionLog(long.Parse(Session["logined_id"].ToString()), "goodprices", "Delete", GV.cn, GV.trans);
                Response.Redirect("Prices.aspx");
            }
            else
            {
                if (Request.QueryString["GID"] != null)
                {
                    LoadGoodPrice();
                }

            }
            if (Request.QueryString["action"] == "deleteGPN" && nPermissions > 7)
            {
                GoodPricesNotice obj = new GoodPricesNotice();
                obj.ID = long.Parse(Request.QueryString["GNID"]);
                obj.Delete(GV.cn, GV.trans);
                UserObjectsLog.SaveObjectActionLog(long.Parse(Session["logined_id"].ToString()), "goodprices", "Delete", GV.cn, GV.trans);
                Response.Redirect("Prices.aspx");
            }
            //Public
            if (Request.QueryString["action"] == "public")
            {
                Prices objPrice = new Prices();
                objPrice.ID = long.Parse(Request.QueryString["ID"].ToString());
                objPrice.LoadExtention(GV.cn, GV.trans);
                string tempDt = "";
                string tempRate = "";
                if (objPrice.DC20.Length > 10)
                {
                    //tempDt = objPrice.DC20.Substring(objPrice.DC20.Length - 10).Trim();
                    tempDt = objPrice.DC20.Split('_')[1].ToString();
                    tempDt = tempDt.Substring(tempDt.Length - 10).Trim();
                    tempRate = objPrice.DC20.Split('_')[0].ToString();
                    GoodPricesNotice.PublicGoodPriceNotice(objPrice, tempDt, tempRate, "20'DC");
                }
                if (objPrice.DC40.Length > 10)
                {
                    //tempDt = objPrice.DC40.Substring(objPrice.DC40.Length - 10).Trim();
                    tempDt = objPrice.DC40.Split('_')[1].ToString();
                    tempDt = tempDt.Substring(tempDt.Length - 10).Trim();
                    tempRate = objPrice.DC40.Split('_')[0].ToString();
                    GoodPricesNotice.PublicGoodPriceNotice(objPrice, tempDt, tempRate, "40'DC");
                }
                if (objPrice.HC40.Length > 10)
                {
                    //tempDt = objPrice.HC40.Substring(objPrice.HC40.Length - 10).Trim();
                    tempDt = objPrice.HC40.Split('_')[1].ToString();
                    tempDt = tempDt.Substring(tempDt.Length - 10).Trim();
                    tempRate = objPrice.HC40.Split('_')[0].ToString();
                    Response.Write("tempDt=" + tempDt + "  tempRate=" + tempRate);
                    GoodPricesNotice.PublicGoodPriceNotice(objPrice, tempDt, tempRate, "40'HC");
                }
                if (objPrice.RF20.Length > 10)
                {
                    //tempDt = objPrice.RF20.Substring(objPrice.RF20.Length - 10).Trim();
                    tempDt = objPrice.RF20.Split('_')[1].ToString();
                    tempDt = tempDt.Substring(tempDt.Length - 10).Trim();
                    tempRate = objPrice.RF20.Split('_')[0].ToString();
                    GoodPricesNotice.PublicGoodPriceNotice(objPrice, tempDt, tempRate, "20'RF");
                }
                if (objPrice.RF40.Length > 10)
                {
                    //tempDt = objPrice.RF40.Substring(objPrice.RF40.Length - 10).Trim();
                    tempDt = objPrice.RF40.Split('_')[1].ToString();
                    tempDt = tempDt.Substring(tempDt.Length - 10).Trim();
                    tempRate = objPrice.RF40.Split('_')[0].ToString();
                    GoodPricesNotice.PublicGoodPriceNotice(objPrice, tempDt, tempRate, "40'RF");
                }
                Response.Redirect("Prices.aspx?PortToID=" + objPrice.PortToID + "&PortFromID=" + objPrice.PortFromID);
            }
            //Pricing
            if (Request.QueryString["action"] != null && Request.QueryString["action"].ToString() == "pricing" && Request.QueryString["pOceanID"] != null)
            {
                Prices objPrice = new Prices();
                objPrice.ID = long.Parse(Request.QueryString["ID"].ToString());
                objPrice.LoadExtention(GV.cn, GV.trans);

                PricingOceanForSales objOceanForSales = new PricingOceanForSales();
                objOceanForSales.ID = long.Parse(Request.QueryString["pOceanID"].ToString());
                objOceanForSales.Load(GV.cn, GV.trans);
                string tempDt = "";
                DateTime dt = DateTime.MinValue;
                DateTime dtNow = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day);
                bool isPricing = false;
                if (objPrice.DC20.Length > 10)
                {
                    //tempDt = objPrice.HC40.Substring(objPrice.HC40.Length - 10).Trim();
                    tempDt = objPrice.DC20.Split('_')[1].ToString();
                    tempDt = tempDt.Substring(tempDt.Length - 10).Trim();
                    try
                    {
                        dt = DateTime.ParseExact(tempDt, "dd-MM-yyyy", CultureInfo.InvariantCulture);
                    }
                    catch { }
                    if (dt == dtNow)
                    {
                        objOceanForSales.DC20 = objPrice.DC20.Split('_')[0];
                        isPricing = true;
                    }
                }
                if (objPrice.DC40.Length > 10)
                {
                    //tempDt = objPrice.HC40.Substring(objPrice.HC40.Length - 10).Trim();
                    tempDt = objPrice.DC40.Split('_')[1].ToString();
                    tempDt = tempDt.Substring(tempDt.Length - 10).Trim();
                    try
                    {
                        dt = DateTime.ParseExact(tempDt, "dd-MM-yyyy", CultureInfo.InvariantCulture);
                    }
                    catch { }
                    if (dt == dtNow)
                    {
                        objOceanForSales.DC40 = objPrice.DC40.Split('_')[0];
                        isPricing = true;
                    }
                }
                if (objPrice.HC40.Length > 10)
                {
                    //tempDt = objPrice.HC40.Substring(objPrice.HC40.Length - 10).Trim();
                    tempDt = objPrice.HC40.Split('_')[1].ToString();
                    tempDt = tempDt.Substring(tempDt.Length - 10).Trim();
                    try
                    {
                        dt = DateTime.ParseExact(tempDt, "dd-MM-yyyy", CultureInfo.InvariantCulture);
                    }
                    catch { }
                    if (dt == dtNow)
                    {
                        objOceanForSales.HC40 = objPrice.HC40.Split('_')[0];
                        isPricing = true;
                    }
                }
                if (objPrice.RF20.Length > 10)
                {
                    //tempDt = objPrice.HC40.Substring(objPrice.HC40.Length - 10).Trim();
                    tempDt = objPrice.RF20.Split('_')[1].ToString();
                    tempDt = tempDt.Substring(tempDt.Length - 10).Trim();
                    try
                    {
                        dt = DateTime.ParseExact(tempDt, "dd-MM-yyyy", CultureInfo.InvariantCulture);
                    }
                    catch { }
                    if (dt == dtNow)
                    {
                        objOceanForSales.RF20 = objPrice.RF20.Split('_')[0];
                        isPricing = true;
                    }
                }
                if (objPrice.RF40.Length > 10)
                {
                    //tempDt = objPrice.HC40.Substring(objPrice.HC40.Length - 10).Trim();
                    tempDt = objPrice.RF40.Split('_')[1].ToString();
                    tempDt = tempDt.Substring(tempDt.Length - 10).Trim();
                    try
                    {
                        dt = DateTime.ParseExact(tempDt, "dd-MM-yyyy", CultureInfo.InvariantCulture);
                    }
                    catch { }
                    if (dt == dtNow)
                    {
                        objOceanForSales.RF40 = objPrice.RF40.Split('_')[0];
                        isPricing = true;
                    }
                }
                if (objPrice.RF40.Length > 10)
                {
                    //tempDt = objPrice.HC40.Substring(objPrice.HC40.Length - 10).Trim();
                    tempDt = objPrice.RF40.Split('_')[1].ToString();
                    tempDt = tempDt.Substring(tempDt.Length - 10).Trim();
                    try
                    {
                        dt = DateTime.ParseExact(tempDt, "dd-MM-yyyy", CultureInfo.InvariantCulture);
                    }
                    catch { }
                    if (dt == dtNow)
                    {
                        objOceanForSales.RF40 = objPrice.RF40.Split('_')[0];
                        isPricing = true;
                    }
                }
                //Schedules
                DataSet ds;
                string condition = " AND CarrID = " + objPrice.CarrID + " AND PortToID=" + objPrice.PortToID + " AND PortFromID=" + objPrice.PortFromID;
                Schedules sd = new Schedules();
                sd.DoSearch(condition, "", out ds, GV.cn, GV.trans);
                string tmpFre = "";
                int tmpTT = 0;
                if (ds.Tables[0].Rows.Count > 0)
                {
                    foreach (DataRow dr in ds.Tables[0].Rows)
                    {
                        sd = new Schedules();
                        sd.ID = long.Parse(dr["ID"].ToString());
                        sd.Load(GV.cn, GV.trans);
                        int Mon = 0, Tue = 0, Web = 0, Thu = 0, Fri = 0, Sat = 0, Sun = 0;
                        try
                        {
                            Mon = int.Parse(sd.Mon);
                        }
                        catch { }
                        try
                        {
                            Tue = int.Parse(sd.Tue);
                        }
                        catch { }
                        try
                        {
                            Web = int.Parse(sd.Web);
                        }
                        catch { }
                        try
                        {
                            Thu = int.Parse(sd.Thu);
                        }
                        catch { }
                        try
                        {
                            Fri = int.Parse(sd.Fri);
                        }
                        catch { }
                        try
                        {
                            Sat = int.Parse(sd.Sat);
                        }
                        catch { }
                        try
                        {
                            Sun = int.Parse(sd.Sun);
                        }
                        catch { }
                        if ((tmpTT > Mon && Mon > 0) || (tmpTT == 0 && Mon > 0))
                        {
                            tmpFre = "Mon";
                            tmpTT = Mon;
                        }
                        if ((tmpTT > Tue && Tue > 0) || (tmpTT == 0 && Tue > 0))
                        {
                            tmpFre = "Tue";
                            tmpTT = Tue;
                        }

                        if ((tmpTT > Web && Web > 0) || (tmpTT == 0 && Web > 0))
                        {
                            tmpFre = "Web";
                            tmpTT = Web;
                        }
                        if ((tmpTT > Thu && Thu > 0) || (tmpTT == 0 && Thu > 0))
                        {
                            tmpFre = "Thu";
                            tmpTT = Thu;
                        }
                        if ((tmpTT > Fri && Fri > 0) || (tmpTT == 0 && Fri > 0))
                        {
                            tmpFre = "Fri";
                            tmpTT = Fri;
                        }
                        if ((tmpTT > Sat && Sat > 0) || (tmpTT == 0 && Sat > 0))
                        {
                            tmpFre = "Sat";
                            tmpTT = Sat;
                        }
                        if ((tmpTT > Sun && Sun > 0) || (tmpTT == 0 && Sun > 0))
                        {
                            tmpFre = "Sun";
                            tmpTT = Sun;
                        }
                    }
                    if (tmpTT > 0)
                    {
                        objOceanForSales.FRE = tmpFre;
                        objOceanForSales.TT_Response = tmpTT.ToString();
                    }
                }
                if (isPricing)
                {
                    objOceanForSales.UserIDResponse = long.Parse(Session["logined_id"].ToString());
                    objOceanForSales.CarrID = objPrice.CarrID;
                    objOceanForSales.Suare = 2;
                    objOceanForSales.Save(GV.cn, GV.trans);
                }
                ClientScript.RegisterStartupScript(typeof(Page), "closePage", "window.close();", true);
                //Response.Redirect("Prices.aspx?PortToID=" + objPrice.PortToID + "&PortFromID=" + objPrice.PortFromID);
            }

            if (isSearch)//((Session["PortToID"].ToString() != "0") && (Session["PortFromID"].ToString() != "0"))
            {
                #region Build List
                int nNumOfPages = 0;

                DataTable dtList;
                BuildList(NumOfItemsPerPage, nPage, out nNumOfPages, out dtList, GV.cn, GV.trans);
                if (nPage > nNumOfPages) nPage = 1;

                rptData.DataSource = dtList;
                rptData.DataBind();
                #endregion
                #region Build Pager
                CommonFunctions.BuildPager(Request, nNumOfPages, nPage, strPageID, ContainerPage, out strPager);
                #endregion
                CommonFunctions.BuildAphabetSearch(Request, ContainerPage, strLetter, strChar, strExcluded, out strAlphaBet);

            }
            else
            {
                if (nPermissionsGoodPrice >= 1 || nPermissions > 9)
                {
                    LoadGoodPriceSearchDropDown();
                    if (Session["sesSerGoodPrices"] != null)
                        ddGoodPriceSer.SelectedValue = Session["sesSerGoodPrices"].ToString();
                    if (Session["sesCountryGoodPrices"] != null)
                        ddGoodPriceCountry.SelectedValue = Session["sesCountryGoodPrices"].ToString();
                    #region Build List
                    int nNumOfPages = 0;

                    DataTable dtList;
                    LoadListGoodPricenNotices(20, nPage, out nNumOfPages, out dtList, GV.cn, GV.trans);
                    if (nPage > nNumOfPages) nPage = 1;
                    #endregion
                    #region Build Pager
                    CommonFunctions.BuildPager(Request, nNumOfPages, nPage, strPageID, ContainerPage, out strPager);
                    #endregion
                }
                LoadListGoodPrice();
                if (nPermissionsGoodPrice < 3)
                {
                    btnUpdateGoodPriceRouting.Enabled = false;
                    btnUpdateGoodPriceRouting.CssClass = "disabled";
                }
                //LoadListGoodPriceRoutingHP();
                //LoadListGoodPriceRoutingHCM();
            }


        }

    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        strError = "";
        strMess = "";
        string sqlCommand = "";
        DataSet ds = null;
        Prices obj = new Prices();
        obj.ID = -1;
        obj.PortToID = long.Parse(Session["PortToID"].ToString());
        obj.PortFromID = long.Parse(Session["PortFromID"].ToString());
        if (obj.PortToID <= 0 && obj.PortFromID <= 0)
        {
            if (Request.QueryString["PortToID"] != null && Request.QueryString["PortFromID"] != null)
            {
                try
                {
                    obj.PortToID = long.Parse(Request.QueryString["PortToID"].ToString());
                    obj.PortFromID = long.Parse(Request.QueryString["PortFromID"].ToString());
                }
                catch { obj.PortToID = -1; obj.PortFromID = -1; };
            }
        }
        if (obj.PortToID > 0 && obj.PortFromID > 0)
        {
            sqlCommand = "select ID,Name from tbl_carrs order by name;";
            ds = DBModule.ExecuteQuery(sqlCommand, GV.cn, GV.trans);
            foreach (DataRow dr in ds.Tables[0].Rows)
            {

                obj.CarrID = long.Parse(dr["ID"].ToString());
                obj.CarrName = dr["Name"].ToString();
                sqlCommand = "select ID from tbl_prices where PortToID=" + obj.PortToID + " and PortFromID=" + obj.PortFromID + " and CarrID=" + obj.CarrID;
                ds = DBModule.ExecuteQuery(sqlCommand, GV.cn, GV.trans);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    Response.Redirect("Prices.aspx?PortToID=" + obj.PortToID + "&PortFromID=" + obj.PortFromID + ((Request.QueryString["page_number"] != null && Request.QueryString["page_number"].ToString() != "") ? "&page_number=" + Request.QueryString["page_number"].ToString() : ""));
                }
                else
                {
                    obj.Save(GV.cn, GV.trans);
                }
            }
        }
        CommonFunctions.GetAllQueryString(Request, "", "id", out strPartQuery);
        Response.Redirect("Prices.aspx" + strPartQuery);
    }
    protected void btnViewAllSchedules_Click(object sender, EventArgs e)
    {

        Response.Redirect("Schedules.aspx?PortToID=" + Session["PortToID"].ToString() + "&PortFromID=" + Session["PortFromID"].ToString());
    }
    protected void btnPricingArea_Click(object sender, EventArgs e)
    {
        Response.Redirect("PriceAreasNew.aspx");
    }
    private void BuildList(int NumOfItemsPerPage, int PageNo, out int nNumOfPages, out DataTable dtList, MySqlConnection cn, MySqlTransaction trans)
    {
        DataSet ds = null;
        dtList = new DataTable();
        dtList.Columns.Add("ID");
        dtList.Columns.Add("Sort");
        dtList.Columns.Add("Index");
        dtList.Columns.Add("CarrID");
        dtList.Columns.Add("PortTo");
        dtList.Columns.Add("PortFrom");
        dtList.Columns.Add("DC20");
        dtList.Columns.Add("DC40");
        dtList.Columns.Add("HC40");
        dtList.Columns.Add("RF20");
        dtList.Columns.Add("RF40");
        //dtList.Columns.Add("TSPort");
        //dtList.Columns.Add("Sailing");
        //dtList.Columns.Add("TT");
        //dtList.Columns.Add("Import");
        dtList.Columns.Add("Validity");
        dtList.Columns.Add("Description");
        dtList.Columns.Add("ValidityEx");
        dtList.Columns.Add("DescriptionEx");
        dtList.Columns.Add("User");
        dtList.Columns.Add("LastUpdate");
        //OTFL
        dtList.Columns.Add("OT20");
        dtList.Columns.Add("OT40");
        dtList.Columns.Add("FL20");
        dtList.Columns.Add("FL40");
        dtList.Columns.Add("CONT45");
        dtList.Columns.Add("ValidityOTFL");
        dtList.Columns.Add("DescriptionOTFL");
        dtList.Columns.Add("Action");
        //Get List by SoIDs
        nNumOfPages = 0;
        string sqlPart = "";
        bool checkFrm = false, checkTo = false;
        string lstPolID = "", lstPodID = "";
        long lPolID = 0, lPodID = 0, lCarrID = 0;
        //Check Condition Frm Input        
        if (Session["PriceFromCountryCode"].ToString().Length > 0)
        {
            sqlPart += " AND CountryCode='" + DBModule.RefineString(Session["PriceFromCountryCode"].ToString()) + "'";
            checkFrm = true;
        }
        if (Session["PriceFromSerAreaCode"].ToString().Length > 0)
        {
            sqlPart += " AND ServiceAreaCode='" + DBModule.RefineString(Session["PriceFromSerAreaCode"].ToString()) + "'";
            checkFrm = true;
        }
        if (Session["PriceFromRegionID"].ToString().Length > 0)
        {
            sqlPart += " AND CountryCode in ( Select CountryCode From tbl_countries WHERE ServiceAreaID=" + DBModule.RefineString(Session["PriceFromRegionID"].ToString()) + ")";
            checkFrm = true;
        }
        if (Session["PortToID"] != null)
        {
            try
            {
                lPolID = long.Parse(Session["PortToID"].ToString());
            }
            catch { }
            if (lPolID > 0)
            {
                sqlPart += " AND ID=" + lPolID;
                checkFrm = true;
            }
        }
        if (checkFrm)
        {
            sqlPart = "Select `ID` from tbl_ports Where 1=1" + sqlPart;
            lstPolID = DBModule.ExecuteQueryForListValue(sqlPart, GV.cn, GV.trans);
            if (lstPolID == null) lstPolID = "-1";
        }
        //Check Condition To Input
        sqlPart = "";
        if (Session["PriceToCountryCode"].ToString().Length > 0)
        {
            sqlPart += " AND CountryCode='" + DBModule.RefineString(Session["PriceToCountryCode"].ToString()) + "'";
            checkTo = true;
        }
        if (Session["PriceToSerAreaCode"].ToString().Length > 0)
        {
            sqlPart += " AND ServiceAreaCode='" + DBModule.RefineString(Session["PriceToSerAreaCode"].ToString()) + "'";
            checkTo = true;
        }
        if (Session["PriceToRegionID"].ToString().Length > 0) // Search Region Code
        {
            sqlPart += " AND CountryCode in ( Select CountryCode From tbl_countries WHERE ServiceAreaID=" + DBModule.RefineString(Session["PriceToRegionID"].ToString()) + ")";
            checkTo = true;
        }
        if (Session["PortFromID"] != null)
        {
            try
            {
                lPodID = long.Parse(Session["PortFromID"].ToString());
            }
            catch { }
            if (lPodID > 0)
            {
                sqlPart += " AND ID=" + lPodID;
                checkTo = true;
            }
        }
        if (checkTo)
        {
            sqlPart = "Select `ID` from tbl_ports Where 1=1" + sqlPart;
            lstPodID = DBModule.ExecuteQueryForListValue(sqlPart, GV.cn, GV.trans);
            if (lstPodID == null) lstPodID = "-1";
        }
        if (Session["PriceCarrID"] != null)
        {
            try
            {
                lCarrID = long.Parse(Session["PriceCarrID"].ToString());
            }
            catch { }
            if (lCarrID > 0)
            {
                strCondition += " AND CarrID=" + lCarrID;
            }
        }
        if (lstPolID.Length > 0 && lstPodID.Length > 0)
        {
            strCondition += " AND PortToID IN (" + lstPolID + ") AND PortFromID IN (" + lstPodID + ")";
        }
        if (int.Parse(Session["PriceTimeUpdate"].ToString()) > 0)
        {
            double addDay = double.Parse(Session["PriceTimeUpdate"].ToString());
            DateTime dtFrom, dtTo;
            if (addDay <= 90)
            {
                dtFrom = DateTime.Now.AddDays(-addDay);
                strCondition += " AND LastUpdate >='" + dtFrom.ToString("yyyy-MM-dd") + "'";
            }
            else
            {
                DateTime dtNow = DateTime.Now;
                string strYear = dtNow.Year.ToString();
                string strMonth = dtNow.Month.ToString();
                string strdtFrom = "";
                string strdtTo = dtNow.ToString("yyyy-MM-dd");
                if (addDay == 500)// 1 --> now
                {
                    strdtFrom = strYear + "-" + strMonth + "-1";
                    strCondition += " AND LastUpdate >='" + strdtFrom + "' AND LastUpdate <='" + strdtTo + "'";
                }
                else if (addDay == 1000)  //15 --> now
                {
                    strdtFrom = strYear + "-" + strMonth + "-15";
                    strCondition += " AND LastUpdate >='" + strdtFrom + "' AND LastUpdate <='" + strdtTo + "'";
                }
                else if (addDay == 1500)  //All quote --> now
                {
                    strCondition += " AND (DC20 <> '' OR DC40 <>'' OR HC40<>'' OR RF20<>'' OR RF40<>'' OR OT20 <> '' OR OT40 <> '' OR FL20 <> '' OR FL40 <> '' OR CONT45 <> '') ";
                }
                else if (addDay == 2000)  //All quote --> All Schedules
                {
                    if (lPodID > 0 && lPolID > 0 && Request.QueryString["action"] != null)
                    {
                        strCondition += " AND ((DC20 <> '' OR DC40 <>'' OR HC40<>'' OR RF20<>'' OR RF40<>'' OR OT20 <> '' OR OT40 <> '' OR FL20 <> '' OR FL40 <> '' OR CONT45 <> '') ";
                        if (Request.QueryString["action"].ToString() == "allschedules_imp")
                            strCondition += " OR CarrID in(SELECT CarrID from tbl_schedules WHERE PortToID=" + lPodID + " AND PortFromID=" + lPolID + "))";//Lich tau van la xuat khau
                        else if (Request.QueryString["action"].ToString() == "allschedules_exp")
                            strCondition += " OR CarrID in(SELECT CarrID from tbl_schedules WHERE PortToID=" + lPolID + " AND PortFromID=" + lPodID + "))"; //Xuat khau
                    }
                }
            }

        }
        if (strCondition == "") strCondition = "All";
        Prices obj = new Prices();
        string strOrderBy = "CarrName,LastUpdate Desc";
        if (int.Parse(Session["PriceTimeUpdate"].ToString()) < 0)
        {
            strOrderBy = "LastUpdate Desc, CarrName";
        }

        Prices.GetList(strCondition, strOrderBy, NumOfItemsPerPage, PageNo, out nNumOfPages, out ds, GV.cn, GV.trans);
        if (ds != null && ds.Tables[0].Rows.Count > 0)
        {
            btnsave.Visible = true;
        }
        else
        {
            if (lPolID > 0 && lPodID > 0 && PageNo == 1)
                btnInit.Visible = true;
        }


        string strAllQueryString = "";

        foreach (string strKey in Request.QueryString.AllKeys)
        {
            if (strKey != null)
            {
                if ((strKey.ToLower() != "action") && (strKey.ToLower() != "id"))
                    strAllQueryString = strAllQueryString + "&" + strKey + "=" + Request.QueryString[strKey].ToString();
            }
        }

        if (strAllQueryString.Length >= 1)
            strAllQueryString = strAllQueryString.Substring(1);

        if (ds != null)
        {
            int count = 0;
            if (Request.QueryString["page_number"] != null && Request.QueryString["page_number"].ToString() != "")
            {
                int pagenumber = 1;
                try
                {
                    pagenumber = int.Parse(Request.QueryString["page_number"]);
                    count = NumOfItemsPerPage * (pagenumber - 1);
                }
                catch { count = 0; }
            }
            DataSet objds = null;

            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                objds = new DataSet();

                count++;
                string strSTT = dr["Sort"].ToString();
                string strIndex = count.ToString();
                string strCarrName = "";
                string CarrName = "";
                string strUser = AdminUser.GetUserNameByID(dr["userid"].ToString(), GV.cn, GV.trans);
                string sqlCommand = "select Name from tbl_carrs where id =" + dr["CarrID"].ToString() + " limit 0,1;";
                objds = DBModule.ExecuteQuery(sqlCommand, GV.cn, GV.trans);
                if (objds.Tables[0].Rows.Count > 0)
                {
                    strCarrName = "<a href=\"#\" ID=\"shedule" + dr["CarrID"].ToString() + "\" onmouseover=\"viewSchedules(" + Session["PortToID"].ToString() + "," + Session["PortFromID"].ToString() + "," + dr["CarrID"].ToString() + ");\">" + objds.Tables[0].Rows[0]["Name"].ToString() + "</a>";
                    CarrName = objds.Tables[0].Rows[0]["Name"].ToString();
                }

                string strPortTo = "";
                sqlCommand = "select Name from tbl_ports where ID =" + dr["PortToID"].ToString() + " limit 0,1;";
                objds = DBModule.ExecuteQuery(sqlCommand, GV.cn, GV.trans);
                if (objds.Tables[0].Rows.Count > 0)
                    strPortTo = objds.Tables[0].Rows[0]["Name"].ToString();

                string strPortFrom = "";
                sqlCommand = "select Name from tbl_ports where id =" + dr["PortFromID"].ToString() + " limit 0,1;";
                objds = DBModule.ExecuteQuery(sqlCommand, GV.cn, GV.trans);
                if (objds.Tables[0].Rows.Count > 0)
                    strPortFrom = objds.Tables[0].Rows[0]["Name"].ToString();
                string[] arrUpdate = new string[0];
                string strID = dr["ID"].ToString();
                string strDC20 = dr["DC20"].ToString();
                string strDC40 = dr["DC40"].ToString();
                string strHC40 = dr["HC40"].ToString();
                string strRF20 = dr["RF20"].ToString();
                string strRF40 = dr["RF40"].ToString();
                //string strTSPort = dr["TSPort"].ToString();
                //string strSailing = dr["Sailing"].ToString();
                //string strTT = dr["TT"].ToString();
                //string strImport = dr["Import"].ToString();
                string strValidity = dr["Validity"].ToString();
                string strDescription = dr["Description"].ToString();
                string strValidityEx = dr["ValidityEx"].ToString();
                string strDescriptionEx = dr["DescriptionEx"].ToString();

                string OT20 = dr["OT20"].ToString();
                string OT40 = dr["OT40"].ToString();
                string FL20 = dr["FL20"].ToString();
                string FL40 = dr["FL40"].ToString();
                string CONT45 = dr["CONT45"].ToString();
                string ValidityOTFL = dr["ValidityOTFL"].ToString();
                string DescriptionOTFL = dr["DescriptionOTFL"].ToString();
                string strLastUpdate = "";
                //if (dr["LastUpdate"].ToString() != "") strLastUpdate = "<font color=\"#0000ff\">" + DateTime.Parse(dr["LastUpdate"].ToString()).ToString("ddMMyyyy") + "</font>";
                if (dr["LastUpdate"].ToString() != "")
                    strLastUpdate = DateTime.Parse(dr["LastUpdate"].ToString()).ToString("ddMMyyyy");
                string strAction = "";
                string strTemp = "";
                strTemp = dr["CarrID"].ToString();
                strTemp = strTemp.Replace("'", "\\'");
                if (nPermissions >= 3)
                {
                    strAction = "<div class=\"changed\">&nbsp;";
                    //get public
                    bool isPublic = false;
                    if (dr["LastUpdate"].ToString().Length > 0)
                    {
                        DateTime dtLst = DateTime.Parse(dr["LastUpdate"].ToString());
                        if (dtLst.AddDays(7) >= DateTime.Now)
                        {
                            strAction = "<a href=\"javascript:doPublic('" + System.Web.HttpUtility.HtmlEncode(dr["ID"].ToString()) + "','" + System.Web.HttpUtility.HtmlEncode(strTemp) + "')\">Public</a>";
                            isPublic = true;
                        }
                        DateTime dt = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day);
                        if (int.Parse(Session["PricingOceanForSales"].ToString()) >= 3 && Request.QueryString["pOceanID"] != null && dtLst == dt)
                        {
                            strAction += " - <a href=\"javascript:doPricing('" + System.Web.HttpUtility.HtmlEncode(dr["ID"].ToString()) + "')\">Pricing</a>";
                        }
                    }

                    //if ((Request.QueryString["PortToID"] != null) && (Request.QueryString["PortFromID"] != null))
                    //{
                    //    if (isPublic) strAction += " - ";
                    //    strAction += "<a href=\"Schedules.aspx?PortToID=" + Request.QueryString["PortToID"].ToString() + "&PortFromID=" + Request.QueryString["PortFromID"].ToString() + "&CarrName=" + CarrName + "\"  target=\"_blank\" >Schedules</a>";
                    //}
                }
                strAction += "</div>";
                //string[] strValues = new string[] { strID, strSTT, strIndex, strCarrName, strPortTo, strPortFrom, strDC20, strDC40, strHC40, strRF20, strRF40, strTSPort, strSailing, strTT, strImport, strValidity, strDescription, strUser, strLastUpdate, strAction };
                string[] strValues = new string[] { strID, strSTT, strIndex, strCarrName, strPortTo, strPortFrom, strDC20, strDC40, strHC40, strRF20, strRF40
                    , strValidity, strDescription, strValidityEx, strDescriptionEx, strUser, strLastUpdate
                    ,OT20,OT40,FL20,FL40,CONT45,ValidityOTFL,DescriptionOTFL,strAction };
                dtList.Rows.Add(strValues);
            }
        }
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        //txtPortToSearch - POL
        //txtPortFromSearch - POD
        bool checkFrm = false, checkTo = false;
        Ports obj = new Ports();
        long lPolID = 0;
        try
        {
            lPolID = long.Parse(hidPolID.Value);
        }
        catch { }
        if (lPolID > 0)
        {
            obj.ID = lPolID;
            obj.Load(GV.cn, GV.trans);

            strPortToName = obj.Name;
            Session["PortToName"] = strPortToName;
            Session["PortToID"] = obj.ID;
            checkFrm = true;
        }
        if (txtPol.Text.Length == 0)
        {
            Session["PortToName"] = "";
            Session["PortToID"] = 0;
        }

        long lPodID = 0;
        try
        {
            lPodID = long.Parse(hidPodID.Value);
        }
        catch { }
        if (lPodID > 0)
        {
            obj = new Ports();
            obj.ID = lPodID;
            obj.Load(GV.cn, GV.trans);
            strPortFromName = obj.Name;
            Session["PortFromName"] = strPortFromName;
            Session["PortFromID"] = obj.ID;
            checkTo = true;
        }
        if (txtPod.Text.Length == 0)
        {
            Session["PortFromName"] = "";
            Session["PortFromID"] = 0;
        }
        //Country        
        if (hidCountryFromSearch.Value.Length > 0)
        {
            Countries ct = new Countries();
            ct.Load(hidCountryFromSearch.Value, GV.cn, GV.trans);
            Session["PriceFromCountryCode"] = ct.CountryCode;
            checkFrm = true;
        }
        if (txtCountryFromSearch.Text.Length == 0) Session["PriceFromCountryCode"] = "";
        if (hidCountryToSearch.Value.Length > 0)
        {
            Countries ct = new Countries();
            ct.Load(hidCountryToSearch.Value, GV.cn, GV.trans);
            Session["PriceToCountryCode"] = ct.CountryCode;
            checkTo = true;
        }
        if (txtCountryToSearch.Text.Length == 0) Session["PriceToCountryCode"] = "";
        //ServiceArea
        if (hidServiceAreaFromSearch.Value.Length > 0)
        {
            ServiceAreas sv = new ServiceAreas();
            sv.LoadByCode(hidServiceAreaFromSearch.Value, GV.cn, GV.trans);
            Session["PriceFromSerAreaCode"] = sv.Code;
            checkFrm = true;
        }
        if (txtServiceAreaFromSearch.Text.Length == 0) Session["PriceFromSerAreaCode"] = "";
        if (hidServiceAreaToSearch.Value.Length > 0)
        {
            ServiceAreas sv = new ServiceAreas();
            sv.LoadByCode(hidServiceAreaToSearch.Value, GV.cn, GV.trans);
            Session["PriceToSerAreaCode"] = sv.Code;
            checkTo = true;
        }
        if (txtServiceAreaToSearch.Text.Length == 0) Session["PriceToSerAreaCode"] = "";
        //Region
        if (hidRegionFromSearch.Value.Length > 0)
        {
            Region rg = new Region();
            rg.ID = long.Parse(hidRegionFromSearch.Value);
            rg.Load(GV.cn, GV.trans);
            Session["PriceFromRegionID"] = rg.ID;
            checkFrm = true;
        }
        if (txtRegionFromSearch.Text.Length == 0) Session["PriceFromRegionID"] = "";
        if (hidRegionToSearch.Value.Length > 0)
        {
            Region rg = new Region();
            rg.ID = long.Parse(hidRegionToSearch.Value);
            rg.Load(GV.cn, GV.trans);
            Session["PriceToRegionID"] = rg.ID;
            checkTo = true;
        }
        if (txtRegionToSearch.Text.Length == 0) Session["PriceToRegionID"] = "";
        //Carr        
        if (checkFrm && checkTo)
        {
            long lCarrID = 0;
            try
            {
                lCarrID = long.Parse(hidCarrID.Value);
            }
            catch { }
            if (lCarrID > 0)
            {
                Carrs carr = new Carrs();
                carr.ID = lCarrID;
                carr.Load(GV.cn, GV.trans);
                Session["PriceCarrID"] = carr.ID;
            }
            if (txtCarr.Text.Length == 0) Session["PriceCarrID"] = 0;
        }
        else
        {
            lblerror.Text = "Bạn phải chọn điêm đi và điểm đến.";
            return;
        }
        Response.Redirect("Prices.aspx");
        //Response.Redirect("Prices.aspx?PortToID=" + Session["PortToID"].ToString() + "&PortFromID=" + Session["PortFromID"].ToString()// + (allquote ? "&allquote=1":"")
        //    + ((Request.QueryString["page_number"] != null && Request.QueryString["page_number"].ToString() != "") ? "&page_number=" + Request.QueryString["page_number"].ToString() : ""));

    }
    private bool LoadSearch()
    {
        //POL
        btnViewAllSchedules.Visible = false;
        bool retval = false, checkFrm = false, checkTo = false;
        if (Session["PortToName"] == null)
            Session["PortToName"] = "";
        if (Session["PortToID"] == null)
            Session["PortToID"] = 0;
        //POD
        if (Session["PortFromName"] == null)
            Session["PortFromName"] = "";
        if (Session["PortFromID"] == null)
            Session["PortFromID"] = 0;
        txtPod.Text = Session["PortFromName"].ToString();
        hidPodID.Value = Session["PortFromID"].ToString();
        txtPol.Text = Session["PortToName"].ToString();
        hidPolID.Value = Session["PortToID"].ToString();
        if (long.Parse(Session["PortToID"].ToString()) > 0)
            checkFrm = true;
        if (long.Parse(Session["PortFromID"].ToString()) > 0)
            checkTo = true;
        if (checkFrm && checkTo) btnViewAllSchedules.Visible = true;
        //Carr
        if (Session["PriceCarrID"] == null)
            Session["PriceCarrID"] = 0;
        long lCarrID = 0;
        try
        {
            lCarrID = long.Parse(Session["PriceCarrID"].ToString());
        }
        catch { }
        if (lCarrID > 0)
        {
            Carrs carr = new Carrs();
            carr.ID = lCarrID;
            carr.Load(GV.cn, GV.trans);
            txtCarr.Text = carr.Name;
            hidCarrID.Value = carr.ID.ToString();
        }

        //Country
        if (Session["PriceFromCountryCode"] == null)
            Session["PriceFromCountryCode"] = "";
        Countries ct = new Countries();
        if (Session["PriceFromCountryCode"].ToString() != "")
        {
            ct.Load(Session["PriceFromCountryCode"].ToString(), GV.cn, GV.trans);
            txtCountryFromSearch.Text = ct.CountryName;
            hidCountryFromSearch.Value = ct.CountryCode;
            checkFrm = true;
        }
        if (Session["PriceToCountryCode"] == null)
            Session["PriceToCountryCode"] = "";
        if (Session["PriceToCountryCode"].ToString() != "")
        {
            ct = new Countries();
            ct.Load(Session["PriceToCountryCode"].ToString(), GV.cn, GV.trans);
            txtCountryToSearch.Text = ct.CountryName;
            hidCountryToSearch.Value = ct.CountryCode;
            checkTo = true;
        }
        //ServiceArea
        if (Session["PriceFromSerAreaCode"] == null)
            Session["PriceFromSerAreaCode"] = "";
        ServiceAreas sv = new ServiceAreas();
        if (Session["PriceFromSerAreaCode"].ToString() != "")
        {
            sv.LoadByCode(Session["PriceFromSerAreaCode"].ToString(), GV.cn, GV.trans);
            txtServiceAreaFromSearch.Text = sv.Code;
            hidServiceAreaFromSearch.Value = sv.Code;
            checkFrm = true;
        }
        if (Session["PriceToSerAreaCode"] == null)
            Session["PriceToSerAreaCode"] = "";
        if (Session["PriceToSerAreaCode"].ToString() != "")
        {
            sv = new ServiceAreas();
            sv.LoadByCode(Session["PriceToSerAreaCode"].ToString(), GV.cn, GV.trans);
            txtServiceAreaToSearch.Text = sv.Code;
            hidServiceAreaToSearch.Value = sv.Code;
            checkTo = true;
        }
        //Region
        if (Session["PriceFromRegionID"] == null)
            Session["PriceFromRegionID"] = "";
        Region rg = new Region();
        if (Session["PriceFromRegionID"].ToString() != "")
        {
            rg.ID = long.Parse(Session["PriceFromRegionID"].ToString());
            rg.Load(GV.cn, GV.trans);
            txtRegionFromSearch.Text = rg.Name;
            hidRegionFromSearch.Value = rg.ID.ToString();
            checkFrm = true;
        }
        if (Session["PriceToRegionID"] == null)
            Session["PriceToRegionID"] = "";
        if (Session["PriceToRegionID"].ToString() != "")
        {
            rg = new Region();
            rg.ID = long.Parse(Session["PriceToRegionID"].ToString());
            rg.Load(GV.cn, GV.trans);
            txtRegionToSearch.Text = rg.Name;
            hidRegionToSearch.Value = rg.ID.ToString();
            checkTo = true;
        }
        if (Session["PriceTimeUpdate"] == null)
            Session["PriceTimeUpdate"] = "1500";

        drPriceUpdateTime.SelectedValue = Session["PriceTimeUpdate"].ToString();
        if (checkFrm && checkTo)
            retval = true;
        return retval;
    }
    private void LoadListGoodPrice()
    {
        DataSet ds = null;
        DataTable dtList = new DataTable();
        dtList.Columns.Add("IndexGoodPrice");
        dtList.Columns.Add("PriceNotices");
        dtList.Columns.Add("User");
        dtList.Columns.Add("LastUpdate");
        dtList.Columns.Add("Action");
        GoodPrices obj = new GoodPrices();
        int numOfPage = 1;
        GoodPrices.GetList("", "LastUpdate DESC, ID DESC", 0, 0, out  numOfPage, out ds, GV.cn, GV.trans);
        int count = 0;
        foreach (DataRow dr in ds.Tables[0].Rows)
        {

            DateTime dt = DateTime.Parse(dr["LastUpdate"].ToString());
            TimeSpan ts = (DateTime.Now - dt);
            if (int.Parse(ts.Days.ToString()) < 30)
            {
                string strPriceNotices = dr["PriceNotices"].ToString();
                if (int.Parse(ts.Days.ToString()) <= 3)
                    strPriceNotices = "<font color=\"Red\">" + dr["PriceNotices"].ToString() + "</font>";
                else if (int.Parse(ts.Days.ToString()) <= 7)
                    strPriceNotices = "<font color=\"#FF7F50\">" + dr["PriceNotices"].ToString() + "</font>";

                count++;
                string strIndexGoodPrice = count.ToString();
                string strLastUpdate = "";
                string strUser = AdminUser.GetUserNameByID(dr["userid"].ToString(), GV.cn, GV.trans);
                if (dr["LastUpdate"].ToString() != "")
                {
                    if (DateTime.Parse(dr["LastUpdate"].ToString()).AddDays(7) >= DateTime.Now)
                        strLastUpdate = "<font color=\"#FF7F50\">" + DateTime.Parse(dr["LastUpdate"].ToString()).ToString("ddMMyyyy") + "</font>";
                    else
                        strLastUpdate = "<font color=\"#0000ff\">" + DateTime.Parse(dr["LastUpdate"].ToString()).ToString("ddMMyyyy") + "</font>";
                }
                string strAction = "";
                if (nPermissions >= 3)
                {
                    strAction = "<a href=\"Prices.aspx?Gid=" + dr["ID"].ToString() + "\">Edit</a>";
                }
                if (nPermissions > 7)
                    strAction += " - <a href=\"javascript:doDeleteGP('" + System.Web.HttpUtility.HtmlEncode(dr["ID"].ToString()) + "')\">Delete</a>";

                string[] strValues = new string[] { strIndexGoodPrice, strPriceNotices, strUser, strLastUpdate, strAction };
                dtList.Rows.Add(strValues);
            }
        }
        rpGoodPrice.DataSource = dtList;
        rpGoodPrice.DataBind();
    }
    protected void btnSaveGoodPrice_Click(object sender, EventArgs e)
    {
        strError = "";
        strMess = "";
        GoodPrices obj = new GoodPrices();
        obj.ID = -1;
        if (Request.QueryString["GID"] != null)
        {
            obj.ID = long.Parse(Request.QueryString["GID"]);
            obj.Load(GV.cn, GV.trans);
        }
        if (txtGoodPrice.Text.Trim() == "")
        {
            strMess = "Good Price not blank!";
            lblerror.Text = strMess;

        }
        else
        {
            obj.PriceNotices = txtGoodPrice.Text.Trim();
            obj.LastUpdate = DateTime.Now;
            obj.UserID = long.Parse(Session["logined_id"].ToString());
            obj.Save(GV.cn, GV.trans);
            if (obj.ID > 0)
                UserObjectsLog.SaveObjectActionLog(long.Parse(Session["logined_id"].ToString()), "goodprices", "Edit", GV.cn, GV.trans);
            else
                UserObjectsLog.SaveObjectActionLog(long.Parse(Session["logined_id"].ToString()), "goodprices", "Add", GV.cn, GV.trans);
            Response.Redirect("Prices.aspx");
        }
    }
    protected void LoadGoodPrice()
    {
        GoodPrices obj = new GoodPrices();
        obj.ID = long.Parse(Request.QueryString["GID"]);
        obj.Load(GV.cn, GV.trans);
        txtGoodPrice.Text = obj.PriceNotices;
    }
    protected void drPriceUpdateTime_SelectedIndexChanged(object sender, EventArgs e)
    {
        Session["PriceTimeUpdate"] = drPriceUpdateTime.SelectedValue;
        if (Session["PriceTimeUpdate"].ToString() == "2000" && (Session["PortToID"].ToString() == "0" || Session["PortFromID"].ToString() == "0"))
        {
            lblerror.Text = "Bạn cần chọn POL và POD.";
            return;
        }
        CommonFunctions.GetAllQueryString(Request, "", "id", out strPartQuery);
        Response.Redirect("Prices.aspx" + strPartQuery);
    }
    public string GetCellTitle(object inputstring)
    {
        string title = "";
        if (inputstring != null && !string.IsNullOrEmpty(inputstring.ToString()))
        {
            string[] arrUpdate = inputstring.ToString().Split('_');
            if (arrUpdate.Length > 2)
            {
                title = "(" + arrUpdate[2] + ") " + arrUpdate[1];
            }
            else if (arrUpdate.Length > 1)
            {
                title = arrUpdate[1];
            }
        }
        return title;
    }
    public string GetCellClass(object inputstring)
    {
        string className = "";
        if (inputstring != null && !string.IsNullOrEmpty(inputstring.ToString()))
        {
            string[] arrUpdate = inputstring.ToString().Split('_');
            if (arrUpdate.Length > 1)
            {
                try
                {
                    string tempDt = arrUpdate[1].Substring(arrUpdate[1].Length - 10).Trim();
                    DateTime dt = DateTime.ParseExact(tempDt, "dd-MM-yyyy", CultureInfo.InvariantCulture);
                    if (dt.AddDays(7) >= DateTime.Now)
                        className = "has-value-lastweek";
                    else if (dt.AddDays(14) >= DateTime.Now && dt.AddDays(7) < DateTime.Now)
                        className = "has-value-last2week";
                    else
                        className = "has-value";
                }
                catch
                {
                    className = "has-value";
                }
            }
        }
        return className;
    }
    public string GetCellValue(object inputstring, bool emptyIfNull = false)
    {
        string cellvalue = !emptyIfNull ? "No check" : "";
        if (inputstring != null && !string.IsNullOrEmpty(inputstring.ToString()))
        {
            string[] arrUpdate = inputstring.ToString().Split('_');
            cellvalue = arrUpdate[0];
        }
        return cellvalue;
    }
    private void LoadListGoodPricenNotices(int NumOfItemsPerPage, int PageNo, out int nNumOfPages, out DataTable dtList, MySqlConnection cn, MySqlTransaction trans)
    {
        DataSet ds = null;
        dtList = new DataTable();
        dtList.Columns.Add("Index");
        dtList.Columns.Add("ID");
        dtList.Columns.Add("CARRNAME");
        dtList.Columns.Add("POL");
        dtList.Columns.Add("POD");
        dtList.Columns.Add("CONT");
        dtList.Columns.Add("RATE");
        dtList.Columns.Add("DOC");
        dtList.Columns.Add("DATEFROM");
        dtList.Columns.Add("DATETO");
        dtList.Columns.Add("NOTE");
        dtList.Columns.Add("USER");
        dtList.Columns.Add("Action");

        int numOfPage = 1;
        string strCondition = "";
        if (Session["sesCountryGoodPrices"] != null || Session["sesSerGoodPrices"] != null)
        {
            string conditionPOL = " POL IN (SELECT NAME FROM tbl_ports WHERE 1=1 ";
            string conditionPOD = " OR POD IN (SELECT NAME FROM tbl_ports WHERE 1=1";
            if (Session["sesCountryGoodPrices"] != null && Session["sesCountryGoodPrices"].ToString() != "")
            {
                conditionPOL += " AND Country='" + DBModule.RefineString(Session["sesCountryGoodPrices"].ToString()) + "'";
                conditionPOD += " AND Country='" + DBModule.RefineString(Session["sesCountryGoodPrices"].ToString()) + "'";
            }
            if (Session["sesSerGoodPrices"] != null && Session["sesSerGoodPrices"].ToString() != "")
            {
                conditionPOL += " AND Region='" + DBModule.RefineString(Session["sesSerGoodPrices"].ToString()) + "'";
                conditionPOD += " AND Region='" + DBModule.RefineString(Session["sesSerGoodPrices"].ToString()) + "'";
            }
            conditionPOL += ")";
            conditionPOD += ")";
            strCondition = " AND ( " + conditionPOL + conditionPOD + ")";
        }
        GoodPricesNotice.GetList(strCondition, "LastUpdate DESC,ID Desc", NumOfItemsPerPage, PageNo, out  nNumOfPages, out ds, GV.cn, GV.trans);
        int count = 0;
        if (Request.QueryString["page_number"] != null && Request.QueryString["page_number"].ToString() != "")
        {
            int pagenumber = 1;
            try
            {
                pagenumber = int.Parse(Request.QueryString["page_number"]);
                count = NumOfItemsPerPage * (pagenumber - 1);
            }
            catch { count = 0; }
        }
        string[] strValues = null;
        foreach (DataRow dr in ds.Tables[0].Rows)
        {

            count++;
            string strIndex = count.ToString();
            string strID = dr["ID"].ToString();
            string CARRNAME = dr["CARRNAME"].ToString();
            string POL = dr["POL"].ToString();
            string POD = dr["POD"].ToString();
            string RATE = float.Parse(dr["RATE"].ToString()).ToString("#,#.##");
            string DOC = dr["DOC"].ToString();
            string CONT = dr["CONT"].ToString();
            string DATEFROM = DateTime.Parse(dr["DateFrom"].ToString()).ToString("MMM-dd-yyyy");
            if (DATEFROM == "Jan-01-0001") DATEFROM = "";
            string DATETO = DateTime.Parse(dr["DateTo"].ToString()).ToString("MMM-dd-yyyy");
            if (DATETO == "Jan-01-0001") DATETO = "";
            string NOTE = dr["NOTE"].ToString();
            string strLastUpdate = "";
            string strUser = "";
            if (dr["userid"].ToString() != "")
                strUser = AdminUser.GetUserNameByID(dr["userid"].ToString(), GV.cn, GV.trans);

            if (dr["LastUpdate"].ToString() != "")
            {

                if (DateTime.Parse(dr["LastUpdate"].ToString()).AddDays(7) >= DateTime.Now)
                    strLastUpdate = "<font color=\"#FF7F50\">" + DateTime.Parse(dr["LastUpdate"].ToString()).ToString("ddMMyyyy") + "</font>";
                else
                    strLastUpdate = "<font color=\"#0000ff\">" + DateTime.Parse(dr["LastUpdate"].ToString()).ToString("ddMMyyyy") + "</font>";

            }
            string strAction = "";
            if (nPermissions > 7)
                strAction = "  <a href=\"javascript:doDeleteGPNotice('" + System.Web.HttpUtility.HtmlEncode(dr["ID"].ToString()) + "')\">Delete</a>";
            strValues = new string[] { strIndex, strID, CARRNAME, POL, POD, CONT, RATE, DOC, DATEFROM, DATETO, NOTE, strUser, strAction };
            dtList.Rows.Add(strValues);
        }
        //strValues = new string[] { "ADD", "-1", "", "", "", "", "", "MBL", "", "", "", null };
        //dtList.Rows.Add(strValues);
        rpGoodPricesNotice.DataSource = dtList;
        rpGoodPricesNotice.DataBind();
    }
    protected void btnUpdateGoodPriceNotices_Click(object sender, EventArgs e)
    {
        //Update HP
        foreach (RepeaterItem item in rpGoodPricesNotice.Items)
        {
            long sID = 0;
            try
            {
                sID = long.Parse(((HiddenField)item.FindControl("ID")).Value.Trim());
            }
            catch { }
            //string Routing = ((TextBox)item.FindControl("Routing")).Text.Trim();
            string POL = ((TextBox)item.FindControl("POL")).Text.Trim();
            string POD = ((TextBox)item.FindControl("POD")).Text.Trim();
            string CARRNAME = ((TextBox)item.FindControl("CARRNAME")).Text.Trim();
            float RATE = 0;
            try
            {
                RATE = float.Parse(((TextBox)item.FindControl("RATE")).Text.Trim());
            }
            catch { }
            string DOC = ((TextBox)item.FindControl("DOC")).Text.Trim();
            string CONT = ((TextBox)item.FindControl("CONT")).Text.Trim();
            string NOTE = ((TextBox)item.FindControl("NOTE")).Text.Trim();
            DateTime DATEFROM = DateTime.Now;
            try
            {
                DATEFROM = DateTime.Parse(((TextBox)item.FindControl("DATEFROM")).Text.Trim());
            }
            catch { }
            DateTime DATETO = DateTime.MinValue;
            try
            {
                DATETO = DateTime.Parse(((TextBox)item.FindControl("DATETO")).Text.Trim());
            }
            catch { }

            if (sID > 0)//Update
            {
                bool isSave = false;
                bool isSaveFRM = false;
                GoodPricesNotice gpr = new GoodPricesNotice();
                gpr.ID = sID;
                gpr.Load(GV.cn, GV.trans);
                if (gpr.POL != POL)
                {
                    gpr.POL = POL;
                    isSave = true;
                }
                if (gpr.POD != POD)
                {
                    gpr.POD = POD;
                    isSave = true;
                }
                if (gpr.CARRNAME != CARRNAME)
                {
                    gpr.CARRNAME = CARRNAME;
                    isSave = true;
                }
                if (gpr.DOC != DOC)
                {
                    gpr.DOC = DOC;
                    isSave = true;
                }
                if (gpr.CONT != CONT)
                {
                    gpr.CONT = CONT;
                    isSave = true;
                }

                if (gpr.RATE != RATE)
                {
                    gpr.RATE = RATE;
                    isSave = true;
                }
                if (gpr.Note != NOTE)
                {
                    gpr.Note = NOTE;
                    isSave = true;
                }
                if (gpr.DateFrom != DATEFROM)
                {
                    gpr.DateFrom = DATEFROM;
                    isSave = true;
                    isSaveFRM = true;
                }
                if (gpr.DateTo != DATETO)
                {
                    gpr.DateTo = DATETO;
                    isSave = true;
                }

                if (isSave)
                {
                    gpr.UserID = long.Parse(Session["logined_id"].ToString());
                    if (!isSaveFRM)
                        gpr.DateFrom = DateTime.Now;
                    gpr.LastUpdate = DateTime.Now;
                    gpr.Save(GV.cn, GV.trans);
                    UserObjectsLog.SaveObjectActionLog(long.Parse(Session["logined_id"].ToString()), "goodprices", "edit", GV.cn, GV.trans);
                }
            }
            else if (CARRNAME.Length > 0)
            {
                Response.Write("CARRNAME=" + CARRNAME);
                GoodPricesNotice gpn = new GoodPricesNotice();
                gpn.ID = sID;
                gpn.POD = POD;
                gpn.POL = POL;
                gpn.CARRNAME = CARRNAME;
                gpn.DOC = DOC;
                gpn.CONT = CONT;
                gpn.RATE = RATE;
                gpn.Note = NOTE;
                gpn.UserID = long.Parse(Session["logined_id"].ToString());
                gpn.DateFrom = DATEFROM;
                if (DATETO == DateTime.MinValue)
                {
                    if (DATEFROM.Day < 14)
                        DATETO = new DateTime(DATEFROM.Year, DATEFROM.Month, 14);
                    else
                    {
                        DATEFROM = DATEFROM.AddMonths(1);
                        DATETO = DATEFROM.AddDays(-(DATEFROM.Day));
                    }
                }
                gpn.DateTo = DATETO;
                gpn.LastUpdate = DateTime.Now;
                gpn.Save(GV.cn, GV.trans);
                UserObjectsLog.SaveObjectActionLog(long.Parse(Session["logined_id"].ToString()), "goodprices", "Add", GV.cn, GV.trans);
            }
        }
        Response.Redirect("Prices.aspx");
    }
    public string GetCellClassForNotice(object inputstring)
    {
        string className = "";
        if (inputstring != null && !string.IsNullOrEmpty(inputstring.ToString()))
        {
            try
            {
                //DateTime dt = DateTime.ParseExact(inputstring.ToString(), "dd-MM-yyyy", CultureInfo.InvariantCulture);
                DateTime dt = DateTime.Parse(inputstring.ToString());
                if (dt.AddDays(7) >= DateTime.Now)
                    className = "has-value-lastweek";
                else if (dt.AddDays(14) >= DateTime.Now && dt.AddDays(7) < DateTime.Now)
                    className = "has-value-last2week";
                else
                    className = "has-value";
            }
            catch
            {
                className = "has-value";
            }

        }
        return className;
    }
    protected void btnGoodPricesSearch_Click(object sender, System.EventArgs e)
    {
        //Session["sesSerGoodPrices"] = POLGoodPrices.Text;
        //Session["sesPODGoodPrices"] = PODGoodPrices.Text;
        //Response.Redirect("Prices.aspx");
    }
    protected void ddGoodPriceSer_SelectedIndexChanged(object sender, EventArgs e)
    {
        Session["sesSerGoodPrices"] = ddGoodPriceSer.SelectedValue;
        Response.Redirect("Prices.aspx");
    }
    protected void ddGoodPriceCountry_SelectedIndexChanged(object sender, EventArgs e)
    {
        Session["sesCountryGoodPrices"] = ddGoodPriceCountry.SelectedValue;
        Response.Redirect("Prices.aspx");
    }
    public void LoadGoodPriceSearchDropDown()
    {
        string sqlCommand = "SELECT Distinct Region FROM tbl_ports WHERE " +
            " NAME IN (SELECT DISTINCT pol FROM tbl_goodprice_notices Where  DATE_ADD(datefrom,INTERVAL 31 DAY) > CURRENT_DATE())" +
            " OR NAME IN (SELECT DISTINCT pod FROM tbl_goodprice_notices Where  DATE_ADD(datefrom,INTERVAL 31 DAY) > CURRENT_DATE()) order by Region";
        DataSet ds = DBModule.ExecuteQuery(sqlCommand, GV.cn, GV.trans);
        ddGoodPriceSer.Items.Add((new ListItem("All Region", "")));
        ddGoodPriceSer.AppendDataBoundItems = true;
        ddGoodPriceSer.DataSource = ds.Tables[0];
        ddGoodPriceSer.DataValueField = ds.Tables[0].Columns[0].ToString();
        ddGoodPriceSer.DataTextField = ds.Tables[0].Columns[0].ToString();
        ddGoodPriceSer.DataBind();
        //POD
        sqlCommand = "SELECT Distinct Country FROM tbl_ports WHERE " +
          " NAME IN (SELECT DISTINCT pol FROM tbl_goodprice_notices Where  DATE_ADD(datefrom,INTERVAL 31 DAY) > CURRENT_DATE())" +
          " OR NAME IN (SELECT DISTINCT pod FROM tbl_goodprice_notices Where  DATE_ADD(datefrom,INTERVAL 31 DAY) > CURRENT_DATE()) order by Country";
        ds = DBModule.ExecuteQuery(sqlCommand, GV.cn, GV.trans);
        ddGoodPriceCountry.Items.Add((new ListItem("All Country", "")));
        ddGoodPriceCountry.AppendDataBoundItems = true;
        ddGoodPriceCountry.DataSource = ds.Tables[0];
        ddGoodPriceCountry.DataValueField = ds.Tables[0].Columns[0].ToString();
        ddGoodPriceCountry.DataTextField = ds.Tables[0].Columns[0].ToString();
        ddGoodPriceCountry.DataBind();
    }
}
